package logic.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


public class DateUtil
{
	private static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm");
	
	private static SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	private static Calendar         c    = Calendar.getInstance();
	
	public static int getYear() {
		return c.get(Calendar.YEAR);
	}
	
	public static int getMonth() {
		return c.get(Calendar.MONTH) + 1;
	}
	
	public static int getDay() {
		return c.get(Calendar.DAY_OF_MONTH);
	}
	
	public static int getHours() {
		return c.get(Calendar.HOUR_OF_DAY);
	}
	
	public static int getMinutes() {
		return c.get(Calendar.MINUTE);
	}
	
	public static void updateCalender(Calendar can) {
		c = can;
	}
	
	/**
	 * 根据月份获得最大天数
	 * 
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @return 最大天数
	 */
	public static int getMaxDayByMonth(int year, int month) {
		Calendar time = Calendar.getInstance();//使用默认时区和语言环境获得一个日历
		//注意：在使用set方法之前，必须先调用clear（），否则很多信息会继承自系统当前的时间
		time.clear();
		time.set(Calendar.YEAR, year);
		time.set(Calendar.MONTH, month);//注意Calendar对象默认一月是为零的
		int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);//获得本月份的天数
		//LogManager.show(tag, year+"年"+month+"月的最大天数是："+day, 1);
		return day;
	}
	
	public static Date getDateTime(String date) throws ParseException {
		return sdf1.parse(date);
	}
	
	public static String getDateTime(Date d) {
		return sdf1.format(d);
	}
	
	public static String getDate(Date d) {
		return sdf2.format(d);
	}
	
	public static String getDate(long d) {
		Date strDate = new Date(d);
		return sdf2.format(strDate);
	}
	
	public static String getTime(Date d) {
		return sdf3.format(d);
	}
	
	public static String getDateToMinuteForDot(Date d) {
		return sdf4.format(d);
	}
	
	public static String getDateToMinuteForDot(long d) {
		Date strDate = new Date(d);
		return sdf4.format(strDate);
	}
	/**
	 * 根据日期获得星期
	 */
	public static String getWeek(Date d) {
		final String dayNames[] = {
		        "星期日",
		        "星期一",
		        "星期二",
		        "星期三",
		        "星期四",
		        "星期五",
		        "星期六"
		};
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(d);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayOfWeek < 0) dayOfWeek = 0;
		return (dayNames[dayOfWeek]);
	}
	
	/**
	 * 消息中心时间显示格式（刚刚、几分钟前、几小时前、昨天、月-日、年-月-日）
	 * 
	 * @param time
	 * @return
	 */
	public static String getShortTime(long time) {
		String shortstring = "";
		if (time == 0) return shortstring;
		
		long now = Calendar.getInstance().getTimeInMillis();
		long datetime = (now - time) / 1000;
		if (datetime > 365 * 24 * 60 * 60)
		{
			shortstring = sdf2.format(new Date(time));
		}
		else if (datetime > 24 * 60 * 60 && ((int) (datetime / (24 * 60 * 60))) == 1)
		{
			shortstring = "昨天";
		}
		else if (datetime > 24 * 60 * 60)
		{
			shortstring = new SimpleDateFormat("MM-dd", Locale.CHINA).format(new Date(time));
		}
		else if (datetime > 60 * 60)
		{
			shortstring = (int) (datetime / (60 * 60)) + "小时前";
		}
		else if (datetime > 60)
		{
			shortstring = (int) (datetime / (60)) + "分钟前";
		}
		else
		{
			shortstring = "刚刚";
		}
		return shortstring;
	}
	
	/**
	 * 企业专区和评论的时间显示格式（刚刚、几分钟前、几小时前、昨天 + 时间、 前天 + 时间 、 普通时间）
	 * 
	 * @param time
	 * @return
	 */
	public static String getRegionTime(long time) {
		String regionTime = "";
		if (time == 0) return regionTime;
		
		Date date = new Date(time); //目标时间
		String strDate = getDate(date);
		
		Calendar calendar = Calendar.getInstance();
		Date curDate = calendar.getTime();//今天
		String strCurDate = getDate(curDate);
	
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		Date b1Date = calendar.getTime();//昨天
		String strB1Date = getDate(b1Date);
		
		calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -2);
		Date b2Date = calendar.getTime();//前天
		String strB2Date = getDate(b2Date);
		
		if (strDate.equalsIgnoreCase(strCurDate))
		{//今天
			calendar = Calendar.getInstance();
			long now = calendar.getTimeInMillis();
			long datetime = (now - time) / 1000;//秒
			if (datetime > 60 * 60)
			{
				regionTime = (int) (datetime / (60 * 60)) + "小时前";
			}
			else if (datetime > 60)
			{
				regionTime = (int) (datetime / (60)) + "分钟前";
			}
			else
			{
				regionTime = "刚刚";
			}
		}
		else if (strDate.equalsIgnoreCase(strB1Date))
		{//昨天
			regionTime = "昨天  " + getTime(date);
		}
		else if (strDate.equalsIgnoreCase(strB2Date))
		{//前天
			regionTime = "前天  " + getTime(date);
		}
		else
		{ //三天前
			regionTime = getDateToMinuteForDot(date);
		}
		return regionTime;
	}

	public static String getFormatedDateTime(long dateTime) {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("HH:mm");
		return sDateFormat.format(new Date(dateTime + 0));
	}
}
